package com.aopcinemas.cinema.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import com.aopcinemas.cinema.model.Hall;
import com.aopcinemas.cinema.model.SeatRow;
import com.aopcinemas.cinema.model.SeatRow.Seat;

public class HallDao {

    private Connection connection;

    public HallDao(Connection connection) {
        this.connection = connection;
    }

    public List<Hall> getHalls() {
        List<Hall> halls = new ArrayList<Hall>();
        HashMap<String, Hall> hallMap = new HashMap<String, Hall>();

        try {
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery("select hall.id, name, row_number, seat_count from hall, seat where hall.id = seat.hall_id");
            while (rs.next()) {
                String name = rs.getString("name");
                Hall hall = hallMap.get(name);
                if (hall == null) {
                    hall = new Hall();
                    hall.setName(name);
                    hall.setId(rs.getLong("id"));
                    hall.setSeatRows(new ArrayList<SeatRow>());
                    hallMap.put(name, hall);
                    halls.add(hall);
                }
                SeatRow seatRow = new SeatRow(rs.getInt("row_number"));
                List<Seat> seats = new ArrayList<Seat>();
                seatRow.setSeats(seats);
                int seatCount = rs.getInt("seat_count");
                for (int i = 0; i < seatCount; i++) {
                    Seat seat = new Seat(i);
                    seats.add(seat);
                }
                Collections.sort(seats);
                hall.getSeatRows().add(seatRow);
            }
            for (Hall hall : halls) {
                Collections.sort(hall.getSeatRows());
            }
            rs.close();
            st.close();
        } catch (SQLException e) {
            throw new RuntimeException("System error", e);
        }

        return halls;

    }

    public Hall getHall(Hall hall) {
        boolean hallIsInitialized = false;

        try {
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery("select hall.id, name, row_number, seat_count from hall, seat where hall.id = seat.hall_id and hall.id = " + hall.getId());
            while (rs.next()) {
                if (!hallIsInitialized) {
                    hall.setName(rs.getString("name"));
                    hall.setSeatRows(new ArrayList<SeatRow>());
                    hallIsInitialized = true;
                }

                SeatRow seatRow = new SeatRow(rs.getInt("row_number"));
                List<Seat> seats = new ArrayList<Seat>();
                seatRow.setSeats(seats);
                int seatCount = rs.getInt("seat_count");
                for (int i = 0; i < seatCount; i++) {
                    Seat seat = new Seat(i);
                    seats.add(seat);
                }
                Collections.sort(seats);
                hall.getSeatRows().add(seatRow);
            }
            Collections.sort(hall.getSeatRows());
            rs.close();
            st.close();
        } catch (SQLException e) {
            throw new RuntimeException("System error", e);
        }
        return hallIsInitialized ? hall : null;
    }

    public Hall getHall(long id) {
        Hall hall = new Hall();
        hall.setId(id);
        return getHall(hall);
    }
}
